package org.nmgyjt.springboot.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.nmgyjt.springboot.beans.admin.DictBean;
import org.nmgyjt.springboot.beans.dto.CsBusinessAuthCodeAddDto;
import org.nmgyjt.springboot.entity.ApiCodeEnum;
import org.nmgyjt.springboot.entity.ApiRes;
import org.nmgyjt.springboot.entity.admin.CsBusiness;
import org.nmgyjt.springboot.entity.admin.CsBusinessAuthCode;
import org.nmgyjt.springboot.service.ICsBusinessAuthCodeService;
import org.nmgyjt.springboot.service.ICsBusinessService;
import org.nmgyjt.springboot.utils.JsonDataSecurity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.Literal;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *对外提供
 */
@RestController
@RequestMapping("/cs/cbac")
@CrossOrigin
@Slf4j
public class CsBusinessAuthCodeController {

    @Autowired
    private ICsBusinessAuthCodeService service;

    @Autowired
    private ICsBusinessService businessService;


    @PostMapping("/addAuthCode")
    public ApiRes addAuthCode(@RequestBody CsBusinessAuthCodeAddDto bean) throws Exception {

        String businessId = bean.getBusinessId();
        String parentBusinessId = bean.getParentBusinessId();

        CsBusiness business = businessService.findByBusinessId(businessId);
        if (business == null){
            return ApiRes.fail(3333,"未配置业务系统");
        }
        List<DictBean> data = bean.getData();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("businessId", businessId);
        jsonObject.put("parentBusinessId", parentBusinessId);
        jsonObject.put("data", data);
        String sign =  JsonDataSecurity.encryptJsonData(jsonObject,business.getSalt());
        log.info("addAuthCode-businessId:{} pBid:{} bean:{}",businessId,parentBusinessId, JSONUtil.toJsonStr(bean));
        if (!sign.equals(bean.getSign())){
            log.info("addAuthCode-sign:{}",sign);
            return ApiRes.fail(ApiCodeEnum.CS_BUSINESS_SIGN_ERR);
        }

        List<String> parentBusinessIdList = new ArrayList<>();

        if (parentBusinessId.contains(",")) {
            String[]  x= parentBusinessId.split(",");
            parentBusinessIdList =  Arrays.asList(x);
        }else {
            parentBusinessIdList.add(parentBusinessId);
        }

        for (String pBid : parentBusinessIdList){

            List<CsBusinessAuthCode> listAdd = new ArrayList<>();
            service.remove(new LambdaQueryWrapper<CsBusinessAuthCode>()
                    .eq(CsBusinessAuthCode::getBusinessId,businessId)
                    .eq(CsBusinessAuthCode::getParentBusinessId,pBid));
            for (DictBean dictBean : data) {
                CsBusinessAuthCode csBusinessAuthCode = new CsBusinessAuthCode();
                csBusinessAuthCode.setBusinessId(businessId);
                csBusinessAuthCode.setParentBusinessId(pBid);
                csBusinessAuthCode.setAuthCode(dictBean.getKey());
                csBusinessAuthCode.setAuthName(dictBean.getValue());
                csBusinessAuthCode.setCreatedTime(new Date());
                listAdd.add(csBusinessAuthCode);
            }
            service.saveBatch(listAdd);

        }

        return ApiRes.ok();
    }



}
